package com.hw.shortlink.project.dao.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hw.shortlink.project.dao.entity.LinkBrowserStats;
import com.hw.shortlink.project.dto.req.ShortLinkGroupStatsReqDto;
import com.hw.shortlink.project.dto.req.ShortLinkStatsReqDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface LinkBrowserStatsMapper extends BaseMapper<LinkBrowserStats> {

    /**
     * 记录短链接访问浏览器数据
     * @param linkBrowserStats
     */
    @Insert("INSERT INTO  tb_link_browser_stats(full_short_url,  date, cnt, browser, create_time, update_time, del_flag) " +
            "VALUES( #{reqDto.fullShortUrl},  #{reqDto.date}, #{reqDto.cnt}, #{reqDto.browser}, " +
            "NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY UPDATE " +
            "cnt = cnt +  #{reqDto.cnt}")
    void shortLinkBrowserStats(@Param("reqDto") LinkBrowserStats linkBrowserStats);


    /**
     * 查询某个短链接的访问浏览器统计数据
     * @param shortLinkStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    browser, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    tb_link_browser_stats " +
            "WHERE " +
            "    full_short_url = #{reqDto.fullShortUrl} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid,browser")
    List<LinkBrowserStats> listBrowserStatsByShortLink(@Param("reqDto") ShortLinkStatsReqDto shortLinkStatsReqDto);


    //todo fix
    /**
     * 查询某个分组的访问浏览器统计数据
     * @param shortLinkGroupStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    browser, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    tb_link_browser_stats " +
            "WHERE " +
            "    gid = #{reqDto.gid} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "   browser")
    List<LinkBrowserStats> listBrowserStatsByGroup(@Param("reqDto")ShortLinkGroupStatsReqDto shortLinkGroupStatsReqDto);
}
